package com.css.zfzx.sjcj.modules.hddcwyCrater.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.zfzx.sjcj.modules.hddcwyCrater.repository.HddcWyCraterNativeRepository;
import com.css.zfzx.sjcj.modules.hddcwyCrater.repository.HddcWyCraterRepository;
import com.css.zfzx.sjcj.modules.hddcwyCrater.repository.entity.HddcWyCraterEntity;
import com.css.zfzx.sjcj.modules.hddcwyCrater.service.HddcWyCraterService;
import com.css.zfzx.sjcj.modules.hddcwyCrater.viewobjects.HddcWyCraterQueryParams;
import com.css.zfzx.sjcj.modules.hddcwyCrater.viewobjects.HddcWyCraterVO;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhangcong
 * @date 2020-12-02
 */
@Service
public class HddcWyCraterServiceImpl implements HddcWyCraterService {

	@Autowired
    private HddcWyCraterRepository hddcWyCraterRepository;
    @Autowired
    private HddcWyCraterNativeRepository hddcWyCraterNativeRepository;

    @Override
    public JSONObject queryHddcWyCraters(HddcWyCraterQueryParams queryParams , int curPage, int pageSize) {
        Page<HddcWyCraterEntity> hddcWyCraterPage = this.hddcWyCraterNativeRepository.queryHddcWyCraters(queryParams
                , curPage, pageSize);
        JSONObject jsonObject = PlatformPageUtils.formatPageData(hddcWyCraterPage);
        return jsonObject;
    }


    @Override
    public HddcWyCraterEntity getHddcWyCrater(String id) {
        HddcWyCraterEntity hddcWyCrater = this.hddcWyCraterRepository.findById(id).orElse(null);
         return hddcWyCrater;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyCraterEntity saveHddcWyCrater(HddcWyCraterEntity hddcWyCrater) {
        String uuid = UUIDGenerator.getUUID();
        hddcWyCrater.setId(uuid);
        hddcWyCrater.setCreateUser(PlatformSessionUtils.getUserId());
        hddcWyCrater.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyCraterRepository.save(hddcWyCrater);
        return hddcWyCrater;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcWyCraterEntity updateHddcWyCrater(HddcWyCraterEntity hddcWyCrater) {
        hddcWyCrater.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcWyCrater.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcWyCraterRepository.save(hddcWyCrater);
        return hddcWyCrater;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcWyCraters(List<String> ids) {
        List<HddcWyCraterEntity> hddcWyCraterList = this.hddcWyCraterRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcWyCraterList) && hddcWyCraterList.size() > 0) {
            for(HddcWyCraterEntity hddcWyCrater : hddcWyCraterList) {
                this.hddcWyCraterRepository.delete(hddcWyCrater);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        List<DictItemEntity> validDictItems = PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
        return validDictItems;
    }

}
